-- UIStoneBox
-- Created by chengb Sep/18/2015
-- 隐蔽的石匣

UIStoneBox = class("UIStoneBox", function()
    return cc.Layer:create();
end);

function UIStoneBox.create(gridData, bookText, bookFun)
    return UIStoneBox.new(gridData, bookText, bookFun);
end

-- 按钮数量
local BTN_COUNT = 6;

-- 构造函数
function UIStoneBox:ctor(gridData, bookText, bookFun)
    self:setName("UIStoneBox");

    -- 复用保险箱建筑界面
    local node = cc.CSLoader:createNode("layout/dungeon/special/StrongBox.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.gridData  = gridData;
    self.bookFun = bookFun;
    self.bookText = bookText;

    -- 点击过的按钮数据
    self.clickData = {};

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIStoneBox:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIStoneBox:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIStoneBox 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIStoneBox:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIStoneBox");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册左下角按钮点击事件
    local btnBook = findChildByName(node, "BT/btn_book");
    if type(self.bookFun) == "function" then
        local function onBookClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_select");
                self.bookFun();
            end
        end
        AddTouchEventListener(btnBook, onBookClick);
        TextStyleM.setTextStyle(btnBook, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        btnBook:setTitleText(self.bookText);
        btnBook:setVisible(true);
    else
        btnBook:setVisible(false);
    end

    -- 注册数字按钮点击事件
    self.buttons = {};
    for i = 1, BTN_COUNT do
        local btn = findChildByName(node, "CT/btn"..i);
        table.insert(self.buttons, btn);
        local function onOkClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_select");
                table.insert(self.clickData, i);
                btn:setTouchEnabled(false);
                btn:setBright(false);

                if #self.clickData == BTN_COUNT then
                    -- 尝试解锁
                    self:tryOpen();
                end
            end
        end
        AddTouchEventListener(btn, onOkClick);
        TextStyleM.setTextStyle(btn, TextStyleM.TEXT_SIZE_MIDDLE2, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        btn:setTitleText(tostring(i));
    end
end

-- 重绘
function UIStoneBox:redraw()
    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    titleLabel:setString(buildingInfo["title"]);
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    iconImg:loadTexture(iconPath);
    local iconSize = iconImg:getContentSize();

    iconImg:setScale(1.3);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    applyString(descLabel, buildingInfo["dialog"]);
end

-- 重置按钮状态
function UIStoneBox:resetButtonsState()
    for _, btn in pairs(self.buttons) do
        btn:setTouchEnabled(true);
        btn:setBright(true);
    end
end

-- 尝试开启
function UIStoneBox:tryOpen()
    -- 获取密码
    local password = FormulaM.invoke("STONE_BOX_PASSWORD", self.gridData);

    -- 判断密码正确性
    for i = 1, BTN_COUNT do
        if self.clickData[i] ~= password[i] then
            canOpen = false;
            break;
        end
    end

    local canContinue = true;
    local len = #self.clickData;
    for i = 1, len do
        -- 只要有一个号码不正确就需要重置
        if self.clickData[i] ~= password[i] then
            canContinue = false;
            break;
        end
    end

    local tip;

    local buildingInfo = BuildingBonusM.queryBuildingView(self.gridData.class);

    if not canContinue then
        -- 提示激活失败
        tip = getLocStr("stone_box_open_failed");

        if GRID_TYPE_HIDE_STRONGBOX == self.gridData.type and buildingInfo then
            tip = buildingInfo["fail_tip"];
        end

        alert(tip);

        -- 按钮重置
        self.clickData = {};
        self:resetButtonsState();
        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UIStoneBox");
        return;
    end

    -- 判断是否点击了所有的按钮
    if #self.clickData ~= #password then
        -- 还有按钮未被点击，让玩家继续
        return;
    end

    -- 所有号码都正确

    -- 隐藏的保险箱
    if GRID_TYPE_HIDE_STRONGBOX == self.gridData.type then
        DungeonActionM.go("take_hide_strongbox", self.gridData:getPos());

        alert(buildingInfo["bonus_tip"]);
    else
        -- 开启石匣
        if true == DungeonActionM.go("open_stone_box", self.gridData:getPos()) then
            alert(getLocStr("stone_box_open_succ"));
        end
    end

    -- 关闭当前界面
    UIMgr.getCurrentScene():removeFormByName("UIStoneBox");
end








